/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.properties.common;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.fireflow.model.AbstractModelElement;
import org.fireflow.model.resourcedef.ResourceDef;
import org.fireflow.model.resourcedef.ResourceType;
import org.fireflow.model.resourcedef.impl.ResourceDefImpl;

/**
 * 创建资源的Dialog
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class CreateResourceDefDialog extends TitleAreaDialog{//extends TrayDialog {
	private static final String MESSAGE = "请输入资源相关的属性";
	ResourceDef resourceDef = null;
	
	ComboViewer  resourceTypeComboBox = null;
	
	Label valueLabel = null;
	Label nameLabel = null;
	Label displayNameLabel = null;
	
	Text valueText = null;
	Text nameText = null;
	Text displayNameText = null;
	Text resourceResolverClzText = null;
	Text resourceResolverBeanText = null;
	
	Button clzSelectButton = null;
	Button beanSelectButton = null;
	Button resourceResolverClzRadio = null;
	Button resourceResolverBeanRadio = null;
	/**
	 * @param parentShell
	 */
	public CreateResourceDefDialog(Shell parentShell,ResourceDef resourceDef) {
		super(parentShell);
		setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX | SWT.TITLE);
				
		this.resourceDef = resourceDef;
		if (resourceDef ==null){
			this.resourceDef = new ResourceDefImpl();
		}
	}

	public ResourceDef getResult(){
		return this.resourceDef;
	}
	
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		Composite resourceEditPanel = new Composite(container,SWT.None);
		resourceEditPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		resourceEditPanel.setLayout(new GridLayout(3,false));
		GridData gridData =  null;
		
		//1、资源类型
		Label resourceTypeLabel = new Label(resourceEditPanel,SWT.None);
		resourceTypeLabel.setText("资源类型：");
		gridData = new GridData();
		resourceTypeLabel.setLayoutData(gridData);
		
		resourceTypeComboBox = new ComboViewer(resourceEditPanel,SWT.DROP_DOWN|SWT.READ_ONLY|SWT.SINGLE);
		resourceTypeComboBox.setContentProvider(new ArrayContentProvider());
		resourceTypeComboBox.setLabelProvider(new LabelProvider(){
			public String getText(Object element) {
				ResourceType resourceType = (ResourceType)element;
				return resourceType == null ? "" : resourceType.getDisplayName();//$NON-NLS-1$
			}
		});
		resourceTypeComboBox.setInput(ResourceType.values());
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		resourceTypeComboBox.getControl().setLayoutData(gridData);
		resourceTypeComboBox.addSelectionChangedListener(new ISelectionChangedListener(){

			public void selectionChanged(SelectionChangedEvent event) {

				updateModel();	
			}
			
		});
		
		//2、值属性
		valueLabel = new Label(resourceEditPanel,SWT.None);
		valueLabel.setText("用户Id：");
		valueLabel.setLayoutData(new GridData());
		
		valueText = new Text(resourceEditPanel,SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		valueText.setLayoutData(gridData);
		valueText.addModifyListener(new ModifyListener(){

			public void modifyText(ModifyEvent e) {
				updateModel();				
			}
			
		});
		

		//name属性
		nameLabel = new Label(resourceEditPanel,SWT.None);
		nameLabel.setText("用户名：");
		nameLabel.setLayoutData(new GridData());
		
		nameText = new Text(resourceEditPanel,SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		nameText.setLayoutData(gridData);
		nameText.addModifyListener(new ModifyListener(){

			public void modifyText(ModifyEvent e) {
				updateModel();				
			}
			
		});
		
		
		//displayName属性
		displayNameLabel = new Label(resourceEditPanel,SWT.None);
		displayNameLabel.setText("中文名：");
		displayNameLabel.setLayoutData(new GridData());
		
		displayNameText = new Text(resourceEditPanel,SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		displayNameText.setLayoutData(gridData);
		displayNameText.addModifyListener(new ModifyListener(){

			public void modifyText(ModifyEvent e) {
				updateModel();				
			}
			
		});
		
		//解析器类
		resourceResolverClzRadio = new Button(resourceEditPanel,SWT.RADIO);
		resourceResolverClzRadio.setText("解析器类：");
		resourceResolverClzRadio.setLayoutData(new GridData());
		resourceResolverClzRadio.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e) {
				resourceResolverBeanText.setEnabled(false);
				beanSelectButton.setEnabled(false);
				
				resourceResolverClzText.setEnabled(true);
				clzSelectButton.setEnabled(true);
			}
		});
		
		resourceResolverClzText = new Text(resourceEditPanel,SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 1;
		resourceResolverClzText.setLayoutData(gridData);
		resourceResolverClzText.addModifyListener(new ModifyListener(){

			public void modifyText(ModifyEvent e) {
				updateModel();				
			}
			
		});
		
		clzSelectButton = new Button(resourceEditPanel,SWT.PUSH);
		clzSelectButton.setText("...");		
		clzSelectButton.setLayoutData(new GridData());
		
		//解析器SpringBean
		resourceResolverBeanRadio = new Button(resourceEditPanel,SWT.RADIO);
		resourceResolverBeanRadio.setText("解析器Bean：");
		resourceResolverBeanRadio.setLayoutData(new GridData());
		resourceResolverBeanRadio.setToolTipText("解析器Spring bean");	
		resourceResolverBeanRadio.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e) {
				resourceResolverClzText.setEnabled(false);
				clzSelectButton.setEnabled(false);
				
				resourceResolverBeanText.setEnabled(true);
				beanSelectButton.setEnabled(true);
			}
		});
		
		resourceResolverBeanText = new Text(resourceEditPanel,SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 1;
		resourceResolverBeanText.setLayoutData(gridData);
		resourceResolverBeanText.addModifyListener(new ModifyListener(){

			public void modifyText(ModifyEvent e) {
				updateModel();				
			}
			
		});
		
		beanSelectButton = new Button(resourceEditPanel,SWT.PUSH);
		beanSelectButton.setText("...");		
		beanSelectButton.setLayoutData(new GridData());

		refresh();
		return parent;
	}
	
	protected void refresh(){
			
		StructuredSelection tmp = new StructuredSelection(this.resourceDef.getResourceType());
		this.resourceTypeComboBox.setSelection(tmp);
		
		this.nameText.setText(this.resourceDef.getName()==null?"":this.resourceDef.getName());
		this.displayNameText.setText(this.resourceDef.getDisplayName()==null?"":this.resourceDef.getDisplayName());
		
		this.valueText.setText(this.resourceDef.getValue()==null?"":this.resourceDef.getValue());

		if (this.resourceDef.getResolverBeanName()!=null 
				&& !this.resourceDef.getResolverBeanName().trim().equals("")){
			this.resourceResolverBeanRadio.setSelection(true);
			this.resourceResolverBeanText.setText(this.resourceDef.getResolverBeanName()==null?"":this.resourceDef.getResolverBeanName());
			resourceResolverBeanText.setEnabled(true);
			beanSelectButton.setEnabled(true);
			
			resourceResolverClzText.setEnabled(false);
			clzSelectButton.setEnabled(false);
		}
		else{
			this.resourceResolverClzRadio.setSelection(true);
			this.resourceResolverClzText.setText(this.resourceDef.getResolverClassName()==null?"":this.resourceDef.getResolverClassName());
			resourceResolverBeanText.setEnabled(false);
			beanSelectButton.setEnabled(false);
			
			resourceResolverClzText.setEnabled(true);
			clzSelectButton.setEnabled(true);
		}
	}
	
	protected void updateModel(){
		updateLabels();
		
		//resourceType
		ISelection selection = resourceTypeComboBox.getSelection();
		if (selection!=null && selection instanceof StructuredSelection){
			ResourceType resourceType = (ResourceType)((StructuredSelection)selection).getFirstElement();
			this.resourceDef.setResourceType(resourceType);
		}else{
			this.resourceDef.setResourceType(null);
		}
		
		//value
		this.resourceDef.setValue(valueText.getText());
		
	
		
		this.resourceDef.setDisplayName(displayNameText.getText());
		
		if (resourceResolverClzRadio.getSelection()){
			this.resourceDef.setResolverClassName(this.resourceResolverClzText.getText());
			this.resourceDef.setResolverBeanName(null);
		}else{
			this.resourceDef.setResolverBeanName(this.resourceResolverBeanText.getText());
			this.resourceDef.setResolverClassName(null);
		}
		if (AbstractModelElement.isValidName(nameText.getText())){
			this.resourceDef.setName(nameText.getText());
		}else{
			this.setErrorMessage("资源名不能为空。");
			return ;
		}
		this.setErrorMessage(null);
		this.setMessage(MESSAGE);
	}
	
	private void updateLabels(){
		ISelection selection = resourceTypeComboBox.getSelection();
		if (selection!=null && selection instanceof StructuredSelection){
			ResourceType resourceType = (ResourceType)((StructuredSelection)selection).getFirstElement();
			if (resourceType==null)return;
			if (resourceType.equals(ResourceType.USER)){
				valueLabel.setText("用户Id：");
				nameLabel.setText("用户名：");
				displayNameLabel.setText("中文名：");
			}else if (resourceType.equals(ResourceType.ROLE)){
				valueLabel.setText("角色Id：");
				nameLabel.setText("角色名：");
				displayNameLabel.setText("中文名：");
			}else if (resourceType.equals(ResourceType.GROUP)){
				valueLabel.setText("用户组Id：");
				nameLabel.setText("用户组名：");
				displayNameLabel.setText("中文名：");
			}else if (resourceType.equals(ResourceType.DEPARTMENT)){
				valueLabel.setText("部门Id：");
				nameLabel.setText("部门名：");
				displayNameLabel.setText("中文名：");
			}else if (resourceType.equals(ResourceType.CUSTOM)){
				valueLabel.setText("资源值：");
				nameLabel.setText("资源名：");
				displayNameLabel.setText("中文名：");
			}else if (resourceType.equals(ResourceType.ACTIVITY_INSTANCE_PERFORMER)){
				valueLabel.setText("Activity Id：");
				nameLabel.setText("资源名：");
				displayNameLabel.setText("中文名：");
			}else if (resourceType.equals(ResourceType.PROCESS_INSTANCE_CREATOR)){
				valueLabel.setText("流程 Id：");
				nameLabel.setText("资源名：");
				displayNameLabel.setText("中文名：");
			}else if (resourceType.equals(ResourceType.VARIABLE_IMPLICATION)){
				valueLabel.setText("变量名：");
				nameLabel.setText("资源名：");
				displayNameLabel.setText("中文名：");
			}else{
				valueLabel.setText("资源值：");
				nameLabel.setText("资源名：");
				displayNameLabel.setText("中文名：");
			}
		}
	}

}
